<?php
/**
 * Created by PhpStorm.
 * User: yys
 * Date: 2016/11/30
 * Time: 22:28
 */

namespace App\Http\Controllers\Backstage;

use App\Http\Controllers\Controller;
use App\Services\CrowdService\CrowdService;
use App\Services\Tool\GeneralTool;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class CrowdController extends Controller
{

    protected $crowdService;
    protected $generalTool;
    /*
    |--------------------------------------------------------------------------
    | CrowdController
    |------------------------------------
    --------------------------------------
    |
    | 人群控制器
    |
    */

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(CrowdService $crowdService,GeneralTool $generalTool)
    {
        $this->crowdService = $crowdService;
        $this->generalTool = $generalTool;
    }


    public function getIndex()
    {
        return redirect('backstage/product-temp/crowd-list');
    }

    /**
     * 获取人群列表
     * @param Request $request
     * @return $this
     */
    public function getCrowdList(Request $request)
    {
        $crowds =  $this->crowdService->getCrowdList($request);
        return view('backstage.crowd.crowdList')->with('crowds',$crowds);
    }

    /**
     * 获取人群列表
     * @param Request $request
     * @return mixed
     */
    public function postJsonCrowdList(Request $request){
        $crowds =  $this->crowdService->getAllCrowdList($request);
        if(empty($crowds)){
           $result["state"] = -1;
           $result["message"] = "失败";
           $result["data"] = array();
         }else{
           $result["state"] = 1;
           $result["message"] = "成功";
           $result["data"] = $crowds;
        }
        return response()->json($result);
    }

    /**
     * 添加人群
     * @param Request $request
     * @return \Illuminate\View\View
     */
    public function getAddCrowd(Request $request){
        return view('backstage.crowd.addCrowd');
    }

    /**
     * 修改人群页面
     * @param Request $request
     * @return \Illuminate\View\View
     */
    public function getUpdateCrowd(Request $request){
        $crowd_id = $request->input("crowd_id");
        $crowd =  $this->crowdService->getCrowdForId($crowd_id);
        if($crowd==null)
        {
            return redirect()->back()->with('error_message', '人群不存在');
        }
        return view('backstage.crowd.updateCrowd')->with('crowd',$crowd);
    }

    /**
     * 修改人群
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function postUpdateCrowd(Request $request)
    {
        $crowd_id = $request->input("crowd_id");
        $crowd =  $this->crowdService->getCrowdForId($crowd_id);
        if($crowd==null)
        {
            return redirect()->back()->with('error_message', '人群不存在');
        }else{
            $crowd_name = $request->input('crowd_name');
            $description = $request->input('description');
            $new_crowd =  array('crowd_name'=>$crowd_name,'description'=>$description);
            $validator =  $this->crowdService->validator($new_crowd);
            $validator->after(function($validator) use ($crowd_id,$crowd_name)
            {
                //判断除当前人群名称外有没有重复人群名称
                $b =  $this->crowdService->getCrowdForNameIsNotCurrent($crowd_id,$crowd_name);
                if($b!=null)
                {
                    $validator->errors()->add('crowd_name', '人群名称已存在!');
                }
            });
            if ($validator->fails())
            {
                $request->flash();
                return redirect()->back()->withErrors($validator->errors())->withInput();;
            }
            $new_crowd['id'] = $crowd_id;
            $num = $this->crowdService->updateCrowd($new_crowd);
            if($num>=1)
            {
                return redirect('backstage/product-temp/crowd-list')->with('success_message', '修改成功');
            }else{
                return redirect()->back()->with('error_message', '未修改如何数据');
            }
        }

    }
    /**
     * 保存
     * @param Request $request
     * @return $this|\Illuminate\Http\RedirectResponse
     */
    public function postAddCrowd(Request $request)
    {
        $crowd_name = $request->input('crowd_name');
        $description = $request->input('description');
        $crowd =  array('crowd_name'=>$crowd_name,'description'=>$description);
        $validator =  $this->crowdService->validator($crowd);
        $validator->after(function($validator) use ($crowd_name)
        {
            //判断除当前人群名称外有没有重复人群名称
            $b =  $this->crowdService->getCrowdForNameIsNotCurrent(0,$crowd_name);
            if($b!=null)
            {
                $validator->errors()->add('crowd_name', '人群名称已存在!');
            }
        });
        if ($validator->fails())
        {
            $request->flash();
            return redirect()->back()->withErrors($validator->errors())->withInput();;
        }
        $this->crowdService->addCrowd($crowd);
        return redirect()->back()->with('success_message','添加成功');
    }

    /**
     * 删除人群
     * @param Request $request
     */
    public function postDeleteCrowd(Request $request)
    {
        $crowd_id = intval($request->input("crowd_id"));
        $num =  $this->crowdService->deleteCrowd($crowd_id);
        if($num===1){
            $result["state"] = 1;
            $result["message"] = "删除成功";
        }else{
            $result["state"] = -1;
            $result["message"] = "删除失败";
        }
        return response()->json($result);
    }
}